Page({
  data: {
    //////*****表单数据实现双向绑定*****///// 
    name:null,
    date: "请输入出生年月",
    age:null,
    site:"",
    ipone:null,
    sex:1,
    profession:"",  ///职业
    index2:"",//医师证certificate

    education:"",////学历
    specialty:"",//专业
    doctordate:"选择从医时间：",
    index1:null,  ////班次下标 classes
  ///////////////***********/////////////////
    docterlist:[], //医师证数据
    classeslist:[], ///班次数据
    
   
    
  },
  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.getdata(options);
  },
  async getdata(options){
       this.getbandata();
      this.getdocterata();
    setTimeout(() => {
        this.getdataid(options.id);
    }, 1000);
    
  },
  getdataid(id){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: getApp().globalData.url+'getdataid/'+id,
      success:res=>{
         let d=res.data
         this.setData({
          name:d.name,
          date:d.date,
          age:d.age,
          site:d.site,
          ipone:d.ipone,
          sex:d.sex,
          profession:d.profession,

          index2:this.indexOf(this.data.docterlist, d.certificate),
    //      certificate:d.certificate,
          education:d.education,
          specialty:d.specialty,
          doctordate:d.doctordate,

          index1:this.indexOf(this.data.classeslist, d.classes),
          //classes:d.classes,
         })
         wx.hideLoading()
         
      }
    })
  },
  indexOf(arr,str){
    let len = arr.length;
    for(let i = 0; i < len; i++){
        // 定位该元素位置
        if(arr[i] == str){
            return i;
        }
    }
    // 数组中不存在该元素
    return -1;
},

  bindPickerChange(e){
    this.setData({
      index1:e.detail.value
    })
    //console.log(this.data.index1)
  },

  //获取医师证数据
  bindPickerChange1(e){
    
    this.setData({
      index2:e.detail.value
    })
   // console.log(this.data.index1)
  },
  yanzheng(){
   let a=1
    if(this.data.name==null ){
      a="请输入姓名"
    }
   else if(this.data.date=="请输入出生年月"){
      a="请输入出生年月"
    }
   else if(this.data.age==null){
      a="请输入年龄"
    }
    else if(this.data.site==""){
      a="请输入地点"
    }
    else if(this.data.ipone==null){
      a="请输入手机号"
    }
    else if(this.data.profession==""){
      a="请输入职业"
    }
    else if(this.data.age<0||this.data.age>120||isNaN(this.data.age)){
      a="年龄错误"
    }
    return a;
  },
/////提交
tijiao(){
//验证
  let yan=this.yanzheng();
  if(yan!=1){
    wx.showToast({
      title: yan,
      icon: 'error',
      duration: 2000
    })
    return 0;
  }
  console.log(this.data.date);
  if(this.data.certificate==true){
    this.setData({
      certificate:"1"
    }) 
  }
  else{
    this.setData({
      certificate:"0"
    }) 
  }
wx.request({
  url: getApp().globalData.url+'adddata',
  //method:"POST",
  data:{
    name:this.data.name,
    date:this.data.date,
    age:this.data.age,
    site:this.data.site,
    ipone:this.data.ipone,
    sex:this.data.sex,
    profession:this.data.profession,
    certificate:this.data.docterlist[this.data.index2],
    education:this.data.education,
    specialty:this.data.specialty,
    doctordate:this.data.doctordate,
    classes:this.data.classeslist[this.data.index1],



  },
  success:res=>{
    if (res.data=="ok"){
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000
    })    }
    else{
      wx.showToast({
        title: '数据格式有误',
        icon: 'error',
        duration: 2000
      })
    }
  }
})
},
//从医时间
doctorChange(e){
  this.setData({
    doctordate:e.detail.value
  })
},
//性别
sexbutton(e){
  console.log(e.detail.value)
 this.setData({
   sex:e.detail.value
 })
},
///有证医师改变
zhengtf(e){
  this.setData({
    certificate: e.detail.value
  })
},

  ///出生年月改变
  DateChange(e){
    let date=new Date();
   let  presentyear=date.getFullYear();

    let cdate=e.detail.value;
    let datey=cdate.split("-")[0];
    
    this.setData({
      age:presentyear-datey
    })
    this.setData({
      date: e.detail.value
    })
  },

  //获取现在班次初始数据
  getbandata(){
   wx.showLoading({
     title: '加载中',
   })
   wx.request({
     url: getApp().globalData.url+'getban/1',
     success:res=>{
    //   console.log(res.data)
       this.setData({
         "classeslist":res.data
       })
      
     }
   })
  },

  getdocterata(){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: getApp().globalData.url+'getban/2',
      success:res=>{
     //   console.log(res.data)
        this.setData({
          "docterlist":res.data
        })
       
      }
    })
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})